home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / util / shell+startup-tools / extracmds / source_etc.lha / src / foreach.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-22  |  3.3 KB  |  101 lines

  1. /*   ---------------------------------      -------     
  2.  *   |\  | | | | |  |.| |   \|  |/ /|\      |||||||     
  3.  *   | | | |/  | |\ |/  |/|  |\ |/  |    ?  ---+---  =< 
  4.  *   | | | |   | |  |     |  |  |   |     \qqqqqqqqq/   
  5.  *   ---------------------------------  ~~~~~~~~~~~~~~~~
  6.  *  foreach() - Calls a function for each of the file names given.
  7.  *              Patterns are expanded to the matching file names.
  8.  *              Directories and soft links are ignored.
  9.  *              The user supplied function is called with the full
  10.  *              path name of each matched file. This may lead to
  11.  *              unexpected results if two or more available volumes
  12.  *              have the same name. The canonical way to do it, is to
  13.  *              pass a lock on the directory containing the file and
  14.  *              have the function temporarily CurrentDir() to it.
  15.  *              Alas, doing that will require a rewrite of almost the
  16.  *              entire package so we will just have to live with it
  17.  *              for now. Maybe in a future release... ;-)
  18.  *
  19.  *  Copyright (C) 1993 Torsten Poulin
  20.  *
  21.  *  This file is part of "Torsten's AmigaDOS Shell Commands" package.
  22.  *  The package is free software; you can redistribute it and/or modify
  23.  *  it under the terms of the GNU General Public License as published by
  24.  *  the Free Software Foundation; either version 2 of the License, or
  25.  *  (at your option) any later version.
  26.  *
  27.  *  The package is distributed in the hope that it will be useful,
  28.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  *  GNU General Public License for more details.
  31.  *
  32.  *  You should have received a copy of the GNU General Public License
  33.  *  along with this program; if not, write to the Free Software
  34.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  *
  36.  *  The author can be contacted by s-mail at
  37.  *    Torsten Poulin
  38.  *    Banebrinken 99, 2, 77
  39.  *    DK-2400 Copenhagen NV
  40.  *    DENMARK
  41.  *
  42.  * $Id: foreach.c,v 1.2 93/03/01 12:59:08 Torsten Rel $
  43.  * $Log:    foreach.c,v $
  44.  * Revision 1.2  93/03/01  12:59:08  Torsten
  45.  * Changed all occurrences of "struct DosBase *" to "struct DosLibrary *"
  46.  * 
  47.  * Revision 1.1  93/03/01  10:59:15  Torsten
  48.  * Initial revision
  49.  * 
  50.  */
  51.  
  52. #include <exec/types.h>
  53. #include <exec/memory.h>
  54. #include <dos/dos.h>
  55. #include <dos/dosasl.h>
  56. #include <clib/dos_protos.h>
  57. #include <clib/exec_protos.h>
  58. #ifdef __SASC
  59. #include <pragmas/dos_pragmas.h>
  60. #include <pragmas/exec_pragmas.h>
  61. #endif
  62. #include "tastlib.h"
  63.  
  64. struct Global {
  65.   struct DosLibrary *DOSBase;
  66. };
  67.  
  68.  
  69. LONG foreach(UBYTE **arg, LONG (*func)(UBYTE *, VOID *), VOID *global)
  70. {
  71.   struct DosLibrary *DOSBase = ((struct Global *) global)->DOSBase;
  72.   struct AnchorPath *ap;
  73.   LONG rc;
  74.  
  75.   if (!(ap = AllocVec(sizeof(struct AnchorPath) + MAXNAMELEN + 1,
  76.               MEMF_PUBLIC | MEMF_CLEAR)))
  77.     return ERROR_NO_FREE_STORE;
  78.   ap->ap_Strlen = MAXNAMELEN;
  79.   ap->ap_BreakBits = SIGBREAKF_CTRL_C;
  80.  
  81.   for (; *arg; arg++)
  82.   {
  83.     if ((rc = MatchFirst(*arg, ap)) == 0)
  84.       do
  85.       {
  86.     if (ap->ap_Info.fib_DirEntryType < 0)
  87.     {
  88.       rc = func(ap->ap_Buf, global);
  89.       if (rc == ERROR_BREAK || rc == ERROR_NO_FREE_STORE)
  90.         break;
  91.     }
  92.       } while ((rc = MatchNext(ap)) == 0);
  93.     MatchEnd(ap);
  94.   }
  95.   FreeVec(ap);
  96.  
  97.   if (rc == ERROR_NO_MORE_ENTRIES)
  98.     rc = RETURN_OK;
  99.   return rc;
  100. }
  101.